package com.yanqu.road.server.logic.activity.condition.pasture;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.ValueParamArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseNewRankActivityCondition;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Objects;

public class PastureMaxScoreCondition extends BaseNewRankActivityCondition {

    public PastureMaxScoreCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        ValueParamArgs args = (ValueParamArgs) object;
        BigInteger score = BigInteger.valueOf(args.getValue());
        if (StringUtils.isNullOrEmpty(getParam())) {
            setParam("0");
        }
        // 上次达到的回合数
        int oldRound = Integer.parseInt(getParam());
        // 新的回合数
        int newRound = Integer.parseInt(args.getParam());
        int compare = score.compareTo(getValue());
        if (compare >= 0) {
            setValue(score);
            // 分数相同取最小回合数
            if (compare == 0) {
                setParam(String.valueOf(Math.min(oldRound, newRound)));
            } else {
                setParam(String.valueOf(newRound));
            }
            //重新生成排行榜
            rankChange();
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.PastureMaxScoreRank.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.PastureMaxScoreRank.getValue(), handle);
    }
}
